package com.nekonomics
package utils

import doobie.*
import doobie.implicits.*
import doobie.free.connection.ConnectionIO
import doobie.hikari.HikariTransactor
import zio.Task
import zio.interop.catz.*

object DoobieUtil {


  extension [T](conn: ConnectionIO[T]) {
    def convertWithXa(xa: HikariTransactor[Task]): Task[T] = conn.transact(xa)
  }


  opaque type DelayedConnectionIOError = () => ConnectionIO[Unit]

   def raise(error: Throwable): DelayedConnectionIOError = {
    () => doobie.FC.raiseError(error)
  }

  extension (delayed: DelayedConnectionIOError) {
    infix def when(cond: =>Boolean): ConnectionIO[Unit] = {
      if cond then delayed()
      else doobie.FC.unit
    }
  }







}
